package com.java.controller;

import com.java.entity.PageBean;
import com.java.entity.PsUser;
import com.java.entity.RespBean;
import com.java.service.PsUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author caiyuan
 * @version 1.0
 * @Date 2020/6/11 13:25 Dream-Seeker
 * @Description
 */
@RestController
@RequestMapping("/user")
@Scope("prototype")
public class PsUserController {

    @Autowired
    PsUserService psUserService;

    @PostMapping("/login")
    public RespBean logins(@RequestParam String userName, @RequestParam String userpassword,String lastLoginTime) {

        //获取用户
        Subject currentuser = SecurityUtils.getSubject();
        //封装Token
        UsernamePasswordToken token = new UsernamePasswordToken(userName, userpassword);
        //验证登录
        try {
            currentuser.login(token);

            Date date = null;
            try {
                date = new SimpleDateFormat("yyyy-MM-dd").parse(lastLoginTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            PsUser user = new PsUser();
            user.setLastLoginTime(date);
            user.setUserName(userName);
            int i = psUserService.setLoginTime(user);
            System.out.println("==========================="+i);
            return RespBean.ok("登录成功", psUserService.getUserbyName(token.getUsername()));
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码不正确");
            return RespBean.error("密码不正确");
        } catch (AuthenticationException e) {
            System.out.println("账号不正确");
            return RespBean.error("账号不正确");
        }
    }

    @GetMapping("/logout")
    public RespBean logout() {
        // 销毁会话
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return RespBean.ok("退出成功！！！");
    }

    @GetMapping("/loginTime")
    public RespBean setLastLoginTime(@RequestParam  String lastLoginTime) throws ParseException {
        Date date = null;
        date = new SimpleDateFormat("yyyy-MM-dd").parse(lastLoginTime);
        PsUser user = new PsUser();
        user.setLastLoginTime(date);
        int i = psUserService.setLoginTime(user);
        return RespBean.ok("退出成功！！！");
    }

    @GetMapping("/userinfo")
    public RespBean getOneUser(@RequestParam String userName, HttpSession session) {
        PsUser user = psUserService.getUserbyName(userName);
        return RespBean.ok("page", user);
    }

    @GetMapping(value = "/allUser")
    public RespBean main(@RequestParam(value = "currentPage", defaultValue = "1", required = false) int currentPage,
                         Model model) {
        PageBean<PsUser> list = psUserService.findByPage(currentPage);//回显分页数据
        if (null != list) {
            return RespBean.ok("page", list);
        } else {
            return RespBean.error("检查sql语句");
        }
    }


}
